﻿// Copyright: 2012
// Author: Minh Vu - YoungJ
// File: Syndication.cs - WindowsPhoneRssReader - WindowsPhoneRssReader
// Time: 8:10 PM 05/08/2012

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using WindowsPhoneRssReader.Models;

namespace WindowsPhoneRssReader.Utils
{
    public class Subscription : MyBase
    {
        #region My Property
        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                NotifyPropertyChanged("Title");
            }
        }
        private string _description;
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                NotifyPropertyChanged("Description");
            }
        }
        private bool _success;
        public bool Success
        {
            get { return _success; }
            set
            {
                _success = value;
                NotifyPropertyChanged("Success");
            }
        }
        #endregion

        public void CheckRss(string url)
        {
            var client = new WebClient();
            client.DownloadStringCompleted += (sender, e) =>
                                                  {
                                                      if (e.Error != null)
                                                      {
                                                          Title = e.Error.Message;
                                                          Description = "Please fix address and try again";
                                                          Success = false;
                                                          return;
                                                      }
                                                      try
                                                      {
                                                          var root = XElement.Parse(e.Result);
                                                          Title = root.Descendants("title").First().Value;
                                                          Description = root.Descendants("description").First().Value;
                                                          Success = true;
                                                      }
                                                      catch (Exception exception)
                                                      {
                                                          Debug.WriteLine("Check Rss: " + exception.Message);
                                                          Title = "not found Xml rss";
                                                          Description = "Please fix address and try again";
                                                          Success = false;
                                                      }
                                                      
                                                  };
            try
            {
                client.DownloadStringAsync(new Uri(url, UriKind.Absolute));
            }
            catch (Exception exception)
            {
                Debug.WriteLine("Check Rss: " + exception.Message);
                Title = exception.Message;
                Description = "Please fix address and try again";
                Success = false;
            }
            
        }
    }
}